package com.cwz.seata.demo2.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cwz.api.entity.SeataDemoCopy;
import com.cwz.api.remote.RemoteSeataDemoService;
import com.cwz.seata.demo2.mapper.SeataDemoCopyMapper;
import com.cwz.seata.demo2.service.SeataDemoCopyService;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Wen
 * @since 2023-10-08
 */
@Service
public class SeataDemoCopyServiceImpl extends ServiceImpl<SeataDemoCopyMapper, SeataDemoCopy> implements SeataDemoCopyService {
	@Autowired
	private SeataDemoCopyMapper seataDemoCopyMapper;
	@Autowired
	private RemoteSeataDemoService remoteSeataDemoService;

	@Override
	@Transactional
	// 重点 第一个开启事务的需要添加 seata 全局事务注解
	@GlobalTransactional
	public int seataDemoTest() {
		// 先修改一下数量
		SeataDemoCopy seataDemoCopy = new SeataDemoCopy();
		seataDemoCopy.setId(1L);
		seataDemoCopy.setNum(15);
		seataDemoCopy.setPrice(new BigDecimal("15"));
		this.seataDemoCopyMapper.updateById(seataDemoCopy);
		// feign
		this.remoteSeataDemoService.updatePrice(1L);
		this.remoteSeataDemoService.addSeataDemo();
		int i = 1/0;
		return 1;
	}
}
